package com.hopes.bizz.risk.integral.bo;

import cn.hutool.core.util.StrUtil;
import com.hopes.bizz.risk.common.constant.HidDicConstant;
import com.hopes.bizz.risk.common.constant.IntegralDicConstant;
import com.hopes.bizz.risk.common.constant.IntegralEventEnum;
import com.hopes.bizz.risk.common.constant.RiskDicConstant;
import com.hopes.bizz.risk.control.entity.RiskMeasureTaskRecordEntity;
import com.hopes.bizz.risk.hid.entity.RiskHidHiddenDangerEntity;
import com.hopes.bizz.risk.integral.entity.RiskIntegralRecordEntity;
import com.hopes.boss.common.core.util.StringUtils;
import lombok.Data;

import java.io.Serializable;
import java.util.*;

/**
 * @author yumr
 * @Description 积分计算BO
 * @time 2023/8/18 11:22
 */
@Data
public class IntegralComputeBO implements Serializable {

    private static final long serialVersionUID = -404443883459864955L;

    // 1、积分事件
    private IntegralEventEnum integralEventEnum;
    // 2、积分事件集合
    private Map<Integer, Integer> integerEventMap;
    // 3、隐患排查
    private RiskHidHiddenDangerEntity riskHidHiddenDanger;
    // 4、双重预防排查任务
    private RiskMeasureTaskRecordEntity riskMeasureTaskRecord;


    public IntegralComputeBO(IntegralEventEnum integralEventEnum,
                             RiskMeasureTaskRecordEntity riskMeasureTaskRecord, Map<Integer, Integer> integerEventMap) {
        this.integerEventMap = integerEventMap;
        this.integralEventEnum = integralEventEnum;
        this.riskMeasureTaskRecord = riskMeasureTaskRecord;
    }

    public IntegralComputeBO(IntegralEventEnum integralEventEnum,
                             RiskHidHiddenDangerEntity riskHidHiddenDanger, Map<Integer, Integer> integerEventMap) {
        this.integerEventMap = integerEventMap;
        this.integralEventEnum = integralEventEnum;
        this.riskHidHiddenDanger = riskHidHiddenDanger;
    }

    public List<RiskIntegralRecordEntity> computeIntegral() {
        switch (integralEventEnum) {
            case hid_report_integral:
                return computeHidReportIntegral();
            case risk_control_integral:
                return computeRiskIntegralRecord();
            case hid_rectify_integral:
                return computeHidRectifiedIntegral();
            default:
                return null;
        }
    }

    private List<RiskIntegralRecordEntity> computeHidRectifiedIntegral() {
        if (Objects.isNull(riskHidHiddenDanger) || Objects.isNull(riskHidHiddenDanger.getDelayStatus())) {
            return null;
        }
        final Date time = Calendar.getInstance().getTime();
        final List<RiskIntegralRecordEntity> result = new ArrayList<>();
        final Integer delayStatus = riskHidHiddenDanger.getDelayStatus();
        // 按时整改完成
        final Integer finishPoints = integerEventMap.get(IntegralDicConstant.HID_INTEGRAL_EVENT_HID_TASK_FINISH_ON_TIME);
        // 超时整改完成
        final Integer finishTimeOutPoints = integerEventMap.get(IntegralDicConstant.HID_INTEGRAL_EVENT_HID_TASK_FINISH_ON_TIMEOUT);

        if ((HidDicConstant.DELAY_STATUS_NORMAL.equals(delayStatus) || HidDicConstant.DELAY_STATUS_APPLY.equals(delayStatus) || HidDicConstant.DELAY_STATUS_REJECT.equals(delayStatus))
                && Objects.nonNull(finishPoints)) {
            // 正常整改 || 延期被驳回、延期中但是提前整改完成
            result.add(buildIntegralRecord(IntegralDicConstant.HID_INTEGRAL_EVENT_HID_TASK_FINISH_ON_TIME, finishPoints, riskHidHiddenDanger.getCorrectiveUserId(), riskHidHiddenDanger.getId(), time));
        } else if (HidDicConstant.DELAY_STATUS_PASS.equals(delayStatus) && Objects.nonNull(finishTimeOutPoints)) {
            // 申请延期通过了
            result.add(buildIntegralRecord(IntegralDicConstant.HID_INTEGRAL_EVENT_HID_TASK_FINISH_ON_TIMEOUT, finishTimeOutPoints, riskHidHiddenDanger.getCorrectiveUserId(), riskHidHiddenDanger.getId(), time));
        }
        return result;
    }

    // 计算隐患上报积分
    private List<RiskIntegralRecordEntity> computeHidReportIntegral() {
        if (Objects.isNull(riskHidHiddenDanger) || Objects.isNull(riskHidHiddenDanger.getHidLevel())) {
            return null;
        }
        final Date time = Calendar.getInstance().getTime();
        final List<RiskIntegralRecordEntity> result = new ArrayList<>();
        final Integer hidLevel = riskHidHiddenDanger.getHidLevel();
        // 上报一般隐患
        final Integer reportNormalPoints = integerEventMap.get(IntegralDicConstant.HID_INTEGRAL_EVENT_REPORT_NORMAL_HIDDEN);
        // 上报重大隐患
        final Integer reportMajorPoints = integerEventMap.get(IntegralDicConstant.HID_INTEGRAL_EVENT_REPORT_MAJOR_HIDDEN);
        if (Objects.nonNull(reportNormalPoints) && HidDicConstant.DANGER_LEVEL_GENERAL.equals(hidLevel)) {
            result.add(buildIntegralRecord(IntegralDicConstant.HID_INTEGRAL_EVENT_REPORT_NORMAL_HIDDEN,
                    reportNormalPoints, riskHidHiddenDanger.getReportUserId(), riskHidHiddenDanger.getId(), time));
        } else if (Objects.nonNull(reportMajorPoints) && HidDicConstant.DANGER_LEVEL_MAJOR.equals(hidLevel)) {
            result.add(buildIntegralRecord(IntegralDicConstant.HID_INTEGRAL_EVENT_REPORT_MAJOR_HIDDEN,
                    reportMajorPoints, riskHidHiddenDanger.getReportUserId(), riskHidHiddenDanger.getId(), time));
        }
        return result;
    }

    /**
     * 双重预防积分事件
     */
    private List<RiskIntegralRecordEntity> computeRiskIntegralRecord() {
        if (Objects.isNull(riskMeasureTaskRecord) || Objects.isNull(riskMeasureTaskRecord.getTaskStatus())) {
            return null;
        }
        final Date time = Calendar.getInstance().getTime();
        final List<RiskIntegralRecordEntity> result = new ArrayList<>();
        final Integer taskStatus = riskMeasureTaskRecord.getTaskStatus();
        final Integer finish = integerEventMap.get(IntegralDicConstant.HID_INTEGRAL_EVENT_RISK_TASK_FINISH_ON_TIME);
        final Integer unFinish = integerEventMap.get(IntegralDicConstant.HID_INTEGRAL_EVENT_RISK_TASK_UNFINISH_ON_TIMEOUT);
        if (Objects.nonNull(finish) && RiskDicConstant.HIDDEN_CHECK_TASK_STATUS_FINISH.equals(taskStatus)) {
            result.add(buildIntegralRecord(IntegralDicConstant.HID_INTEGRAL_EVENT_RISK_TASK_FINISH_ON_TIME,
                    finish, riskMeasureTaskRecord.getTaskHandleUserId(), riskMeasureTaskRecord.getId(), time));
        } else if (Objects.nonNull(unFinish) && RiskDicConstant.HIDDEN_CHECK_TASK_STATUS_EXPIRE.equals(taskStatus)) {
            final String responsibleUserIds = riskMeasureTaskRecord.getResponsibleUserIds();
            if (StrUtil.isNotEmpty(responsibleUserIds)) {
                Arrays.stream(responsibleUserIds.split(StringUtils.COMMA)).forEach(item -> {
                    result.add(buildIntegralRecord(IntegralDicConstant.HID_INTEGRAL_EVENT_RISK_TASK_UNFINISH_ON_TIMEOUT,
                            unFinish, Long.parseLong(item), riskMeasureTaskRecord.getId(), time));
                });
            }
        }
        return result;
    }

    private RiskIntegralRecordEntity buildIntegralRecord(Integer event, Integer integral, Long account, Long bizId, Date date) {
        final RiskIntegralRecordEntity record = new RiskIntegralRecordEntity();
        record.setBizId(bizId);
        record.setEventName(event);
        record.setAccountId(account);
        record.setIntegral(integral);
        record.setEventTime(date);
        record.setStatisticalDate(date);
        return record;
    }

}
